package com.xinhe.web.reposity;

import com.xinhe.web.entity.TeamBuy;
import com.xinhe.web.response.TeamBuyOrderMemberView;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Map;

public interface TeamBuyRepository  extends JpaRepository<TeamBuy, Long> {

    @Query(value = "select * from tb_team_buy where status = 'TEAMING' and date_add(start_time, interval ?1 hour) < now()", nativeQuery = true)
    List<TeamBuy> findByTimeout(Integer teamTimeoutHour);
    @Query(value = "select * from tb_team_buy where removed = 0 and f_leader_user_id = ?1 and status = ?2 and f_goods_id =?3", nativeQuery = true)
    TeamBuy findOneByLeaderUserIdAndStatus(Long userId, String teaming, Long teamGoodsId);

    @Query(value = "select m.f_user_id userId,m.nickname,m.icon_path iconPath from tb_team_buy tb " +
            "inner join tb_team_buy_order t on t.removed = 0 and if(tb.f_leader_user_id = t.f_buyer_user_id,1,t.status in ('PAYED','SUCCESS')) and t.f_team_buy_id = tb.id " +
            "inner join tb_member m on m.f_user_id = t.f_buyer_user_id where tb.removed = 0 and tb.id = ?1 " +
            "order by if(m.f_user_id = tb.f_leader_user_id,1,0) desc,t.create_time desc ", nativeQuery = true)
    List<Map<String, Object>> findMemberList(Long id);
}
